<template>
  <div>
    <tableComponents
      :pagination="{currentPage,pageSize:pagesize,total:cate_list.all_num}"
      @handleCurrentChange="handleCurrentChange"
      @handleSizeChange="handleSizeChange"
      @sendUserSearch="receive_search"
      :searchData="userSearchData"
      :isHaveExport="false"
      @toAdd="add_cate"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="cate_list.data"
          v-loading="loading"
          element-loading-text="拼命加载中"
          stripe
          header-row-class-name="head_color"
          tooltip-effect="dark"
          style="width: 99.8%"
        >
          <el-table-column label="类别封面" min-width="80">
            <template slot-scope="scope">
              <div style="width: 120px; padding:10px;">
                <img style="width:100%" :src="$fnc.getImgUrl(scope.row.cate_pic)" alt />
              </div>
            </template>
          </el-table-column>
          <el-table-column label="类别名称" min-width="250">
            <template slot-scope="scope">{{scope.row.cate_name}}</template>
          </el-table-column>
          <el-table-column label="创建时间" min-width="80">
            <template slot-scope="scope">{{scope.row.created_time |getTimeFormat}}</template>
          </el-table-column>

          <el-table-column label="操作" min-width="150">
            <template slot-scope="scope">
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="primary"
                @click="update_cate(scope.row)"
              >编辑</el-button>
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="danger"
                @click="delete_cate( scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </tableComponents>

    <el-dialog
      :close-on-click-modal="false"
      title="添加课程"
      v-loading="dioLoading"
      class="tl size_13"
      width="800px"
      :visible.sync="dialogaddVisible"
    >
      <el-form :model="add_form" ref="form" :label-width="formLabelWidth">
        <el-form-item label="类别名称：">
          <el-col :span="12">
            <el-input
              type="text"
              clearable
              placeholder="请输入"
              v-model="add_form.cate_name"
              autocomplete="off"
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="类别封面：">
          <el-col :span="12">
            <el-input
              v-model="add_form.cate_pic"
              autocomplete="off"
              disabled="disabled"
              class="mar_bottom_5"
            ></el-input>

            <uploadpic
              :piclink="add_form.cate_pic"
              :width="150"
              :height="150"
              :is_more="false"
              @send_pic="get_pic"
            ></uploadpic>
          </el-col>
        </el-form-item>

        <el-form-item>
          <el-button :loading="$store.state.isLoading" type="primary" @click="add_submit()">添 加</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog
      :close-on-click-modal="false"
      title="编辑课程分类"
      v-loading="dioLoading"
      class="tl size_13"
      width="800px"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="cate_single" ref="form" :label-width="formLabelWidth">
        <el-form-item label="类别名称：">
          <el-col :span="12">
            <el-input
              type="text"
              :rows="6"
              clearable
              placeholder="请输入"
              v-model="cate_single.cate_name"
              autocomplete="off"
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="类别封面：">
          <el-col :span="12">
            <el-input
              v-model="cate_single.cate_pic"
              autocomplete="off"
              disabled="disabled"
              class="mar_bottom_5"
            ></el-input>
            <uploadpic
              :piclink="cate_single.cate_pic"
              :width="150"
              :height="150"
              :is_more="false"
              @send_pic="get_pic2"
            ></uploadpic>
          </el-col>
        </el-form-item>

        <el-form-item>
          <el-button :loading="$store.state.isLoading" type="primary" @click="edit_submit()">编 辑</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import uploadpic from "@/components/page/pic/up_pic";
import { courseCateSearchData } from "@/assets/js/searchData/searchData"; //引入搜索的数据
export default {
  name: "cate",
  data() {
    return {
      userSearchData: courseCateSearchData,
      up_show2: false,
      up_show: false,
      is_add: true,
      add_form: {
        cate_name: "",
        cate_pic: ""
      },
      cate_option: [],
      cate_single: {},
      dialogaddVisible: false,
      dialogFormVisible: false,
      dioLoading: false,
      // multipleSelection: [],
      cate_list: {
        data: []
      },
      loading: false,
      currentPage: 1, //初始页
      pagesize: 10, //    每页的数据
      serarch_from: {},
      formLabelWidth: "120px"
    };
  },
  components: {
    uploadpic
  },
  created() {
    this.get_cate_list();
  },
  methods: {
    get_pic2(pic) {
      this.cate_single.cate_pic = pic;
    },
    get_pic(pic) {
      this.add_form.cate_pic = pic;
    },
    onSuccess(pic) {
      if (this.is_add) {
        this.add_form.cate_pic = pic;
      } else {
        this.cate_single.cate_pic = pic;
      }
    },
    add_submit() {
      var params = this.add_form;
      this.$api.getShops.add_catelist(params).then(res => {
        if (res.data.code == 200) {
          this.$fnc.alertSuccess("添加成功");
          this.dialogaddVisible = false;
          this.get_cate_list();
        } else {
          this.$fnc.alertError(res.data.result);
          this.get_cate_list();
        }
      });
    },
    add_cate() {
      this.is_add = true;
      this.add_form = {
        cate_name: "",
        cate_pic: ""
      };
      this.dialogaddVisible = true;
    },
    edit_submit() {
      var params = {};
      params = this.cate_single;
      params.cate_name = this.cate_single.cate_name;
      params.cate_pic = this.cate_single.cate_pic;

      this.$api.getShops.edit_catelist(params).then(res => {
        if (res.data.code == 200) {
          this.$fnc.alertSuccess("修改成功");
          this.dialogFormVisible = false;
          this.get_cate_list();
        } else {
          this.$fnc.alertError(res.data.result);
          this.get_cate_list();
        }
      });
    },
    delete_cate(id) {
      //删除
      this.$swal({
        title: "您确定要删除这些信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          this.$api.getShops.del_catelist({ id: id }).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.get_cate_list();
            } else {
              this.$swal("删除失败！", res.data.result, "error");
              this.get_cate_list();
            }
          });
        }
      });
    },
    update_cate(item) {
      this.is_add = false;
      this.dialogFormVisible = true;
      this.cate_single = item;
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    receive_search(form) {
      this.serarch_from = form;
      this.get_cate_list(form);
    },
    get_cate_list() {
      this.loading = true;
      this.$api.getShops.get_selcate({}).then(res => {
        if (res.data.code == 200) {
          this.cate_option = res.data.result;
          this.loading = false;
        } else {
          this.$fnc.alertError(res.data.result);
          this.loading = false;
        }
      });
    },
    get_cate_list(data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;
      this.$api.getShops.get_catelist(params).then(res => {
        if (res.data.code == 200) {
          this.cate_list = res.data.result;
          this.loading = false;
        } else {
          this.$fnc.alertError(res.data.result);
          this.loading = false;
        }
      });
    },
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function(size) {
      //每页下拉显示数据
      var params = this.serarch_from;
      params.page = "1";
      params.page_size = size;
      this.get_cate_list(params);
      this.pageSize = size;
    },
    handleCurrentChange: function(currentPage) {
             this.currentPage = currentPage;
      var params = this.serarch_from;
      params.page = currentPage;
      params.page_size = this.pageSize;
      this.get_cate_list(params);
    }
  }
};
</script>
<style scoped>
.vide0-con {
  background-color: transparent !important;
  border: none;
  box-shadow: none;
  width: 600px;
}
.vide0-con .el-dialog__header {
  display: none;
}
.bannger_img {
  /* height: auto !important; */
}
</style>
